package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    //创建学生方法
    private StudentDao studentDao=new StudentDao();
    public boolean addStudent(Student student){
        //将返还boolean的值类型结果 返还给 StudentController
        return studentDao.addStudent(student);
    }
    //判断学号是否存在的方法？
    public boolean isExists(String id){
        Student[] students=studentDao.findAllStudent();
        //假设学号在数组中不存在！
        boolean exists=false;
        //遍历数组 获取每一个 学生对象 判断！
        for (int i=0;i<students.length;i++){
            Student student=students[i];
            if (student!=null && student.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }
    //查看学生的方法
    public  Student[] findAllStudent(){
        //调用库管的对象方法  获取对象数组
        Student[] allStudent=studentDao.findAllStudent();
        //2判断 数组中是否 有学生的信息 有 返回地址 无 null
        boolean flag=false;
        for (int i=0;i<allStudent.length;i++){
            Student student=allStudent[i];
            if (student!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            return  allStudent;
        }
        else {
            return null;
        }
    }
    public  void deleteStudentById(String delId){
        studentDao.deleteStudentById(delId);
    }
    public  void updateStudent(String updateId,Student newStudent){
        studentDao.updateStudent(updateId,newStudent);
    }
}
